package com.dmap.admin.service.backgroundv1.impl;

import com.dmap.admin.bo.user.UserInfo;
import com.dmap.admin.controller.background.param.UserParam;
import com.dmap.admin.dao.AdminUserMapper;
import com.dmap.admin.po.AdminRole;
import com.dmap.admin.po.AdminUser;
import com.dmap.admin.po.AdminUserExample;
import com.dmap.admin.service.backgroundv1.RoleService;
import com.dmap.admin.service.backgroundv1.UserService;
import com.dmap.admin.vo.UserVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:46
 * \* Description:
 * \
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private RoleService roleService;

    @Override
    public boolean insertUser(UserParam param) {
        AdminUser adminUser = new AdminUser();
        adminUser.setAccount(param.getAccount());
        adminUser.setPassword(param.getPassword());
        adminUser.setRoleId(Long.valueOf(param.getRoleId()));
        adminUser.setNickname(param.getNickname());
        adminUser.setHeadImageUrl(param.getHeadImageUrl());
        adminUser.setStatus(1);
        return adminUserMapper.insertSelective(adminUser) == 1;
    }

    @Override
    public boolean updateUser(UserParam param) {
        AdminUser adminUser = new AdminUser();
        adminUser.setId(Long.valueOf(param.getId()));
        adminUser.setAccount(param.getAccount());
        adminUser.setPassword(param.getPassword());
        adminUser.setRoleId(Long.valueOf(param.getRoleId()));
        adminUser.setNickname(param.getNickname());
        adminUser.setHeadImageUrl(param.getHeadImageUrl());
        return adminUserMapper.updateByPrimaryKeySelective(adminUser) == 1;
    }

    @Override
    public boolean deleteUser(long userId) {
        return userId != 1 && adminUserMapper.deleteByPrimaryKey(userId) == 1;
    }

    @Override
    public UserVo selectUserInfo(String account, Integer pageIndex, Integer pageSize) {
        AdminUserExample adminUserExample = new AdminUserExample();
        AdminUserExample.Criteria criteria = adminUserExample.createCriteria();
        if (StringUtils.isNotEmpty(account)) {
            criteria.andAccountLike("%" + account + "%");
        }
        int count = adminUserMapper.countByExample(adminUserExample);
        adminUserExample.setLimitStart((pageIndex - 1) * pageSize);
        adminUserExample.setLimitEnd(pageSize);
        List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserExample);
        List<UserInfo> userInfos = new ArrayList<>(pageSize);
        adminUserList.forEach(adminUser -> {
            UserInfo userInfo = new UserInfo(adminUser);
            AdminRole role = roleService.selectRoleById(adminUser.getRoleId());
            if (role != null) {
                userInfo.setRoleName(role.getName());
            }
            userInfos.add(userInfo);
        });
        UserVo userVo = new UserVo();
        userVo.setTotal(count);
        userVo.setUserInfoList(userInfos);
        return userVo;
    }

    @Override
    public AdminUser selectByAccount(String account, String password) {
        AdminUserExample adminUserExample = new AdminUserExample();
        AdminUserExample.Criteria criteria = adminUserExample.createCriteria();
        criteria.andAccountEqualTo(account);
        if (StringUtils.isNotEmpty(password)) {
            criteria.andPasswordEqualTo(password);
        }
        List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserExample);
        if (CollectionUtils.isNotEmpty(adminUserList)) {
            return adminUserList.get(0);
        }
        return null;
    }

    @Override
    public boolean isExistSelectByAccount(String account) {
        AdminUserExample adminUserExample = new AdminUserExample();
        AdminUserExample.Criteria criteria = adminUserExample.createCriteria();
        criteria.andAccountEqualTo(account);
        List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserExample);
        return CollectionUtils.isNotEmpty(adminUserList);
    }

    @Override
    public AdminUser selectUserById(long id) {
        return adminUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public UserInfo selectUserByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        AdminUserExample adminUserExample = new AdminUserExample();
        AdminUserExample.Criteria criteria = adminUserExample.createCriteria();
        criteria.andAccountEqualTo(account);
        List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserExample);
        if (CollectionUtils.isEmpty(adminUserList)) {
            return null;
        }
        UserInfo userInfo = new UserInfo(adminUserList.get(0));
        AdminRole role = new AdminRole();
        if (adminUserList.get(0).getRoleId() != null) {
            role = roleService.selectRoleById(adminUserList.get(0).getRoleId());
        }
        if (role != null) {
            userInfo.setRoleName(role.getName());
        }
        return userInfo;
    }
}
